package com.tzy.rabbitmq.utils;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
 * 获取RabbitMQ连接
 * @Author 自律且自强ing
 * @Created
 */
public class RabbitmqUtil {

    /**
     * 获取RabbitMQ连接
     * @return
     * @throws IOException
     * @throws TimeoutException
     */
    public static Connection getConnection() throws IOException, TimeoutException {
        //构建一个工厂
        final ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置主机信息
//        connectionFactory.setHost("localhost");
//        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setHost("192.168.2.115");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("mqadmin");
        connectionFactory.setPassword("mqadmin");
        connectionFactory.setVirtualHost("/");
        //获取TCP长连接
        return connectionFactory.newConnection();
    }

//    public static void main(String[] args) throws IOException, TimeoutException {
//        ConnectionFactory connectionFactory = new ConnectionFactory();
//        connectionFactory.setHost("localhost");
//        connectionFactory.setPort(5672);
//        connectionFactory.setUsername("admin");
//        connectionFactory.setPassword("admin");
//        connectionFactory.setVirtualHost("/");
//        try (Connection connection = connectionFactory.newConnection();
//             Channel channel = connection.createChannel()) {
//
//        }
//    }
    //    @Value("${rabbitmq.host}")
//    private static String host;
//    @Value("${rabbitmq.port}")
//    private static int port;
//    @Value("${rabbitmq.username}")
//    private static String username;
//    @Value("${rabbitmq.password}")
//    private static String password;
//    @Value("${rabbitmq.virtualHost}")
//    private static String virtualHost;
//    public static Connection getConnection() throws IOException, TimeoutException {
//        System.out.println("host="+host);
//        //构建一个工厂
//        final ConnectionFactory connectionFactory = new ConnectionFactory();
//
//        //设置主机信息
//        connectionFactory.setHost(host);
//        connectionFactory.setPort(port);
//        connectionFactory.setUsername(username);
//        connectionFactory.setPassword(password);
//        connectionFactory.setVirtualHost(virtualHost);
//        //获取TCP长连接
//        return connectionFactory.newConnection();
//    }
}
